package com.itwlj.mapper;

import com.github.pagehelper.Page;
import com.itwlj.annotation.AutoFill;
import com.itwlj.dto.SubCategoryPageQueryDTO;
import com.itwlj.entity.SubCategory;
import com.itwlj.enumeration.OperationType;
import com.itwlj.vo.SubCategoryVO;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface SubCategoryMapper {

    /**
     * 根据分类id查询子分类数量
     *
     * @param categoryId
     * @return
     */
    @Select("select count(*) from sub_category where category_id = #{categoryId}")
    Integer countByCategoryId(Long categoryId);

    /**
     * 新增子分类
     *
     * @param subCategory
     */
    @Insert("insert into sub_category(name, category_id, picture, create_time, update_time, create_user, update_user) VALUES " +
            "(#{name},#{categoryId},#{picture},#{createTime},#{updateTime},#{createUser},#{updateUser})")
    @AutoFill(OperationType.INSERT)
    void insert(SubCategory subCategory);

    /**
     * 子分类分页查询
     *
     * @param subCategoryPageQueryDTO
     * @return
     */
    Page<SubCategoryVO> pageQuery(SubCategoryPageQueryDTO subCategoryPageQueryDTO);

    /**
     * 子分类批量删除
     *
     * @param ids
     */
    void deleteBatch(List<Long> ids);

    /**
     * 根据id修改子分类
     *
     * @param subCategory
     */
    @Update("update sub_category set name = #{name} ,category_id = #{categoryId} ,picture = #{picture} ,update_time = #{updateTime},update_user = #{updateUser} where id = #{id}")
    @AutoFill(value = OperationType.UPDATE)
    void update(SubCategory subCategory);

    /**
     * 查询分类
     *
     * @return
     */
    @Select("select id, name from sub_category")
    List<SubCategory> list();

    /**
     * 根据分类id查询子分类
     *
     * @param categoryId
     * @return
     */
    @Select("select id, name, picture from sub_category where category_id = #{categoryId}")
    List<SubCategory> getByCategoryId(Long categoryId);

    /**
     * 根据id查询子分类
     *
     * @param id
     * @return
     */
    @Select("select * from sub_category where id = #{id}")
    SubCategory getById(Long id);
}
